﻿using AutoMapper;
using BPMN.Application.Contracts.DTO.Request.Material;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.Contracts.DTO.Response.Material;
using BPMN.Application.IService;
using BPMN.Domain.Entity.Material;
using BPMN.Domain.Entity.Unit;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.Application.Service
{
    public class MaterialService : AppServiceBase<Material> , IMaterialService
    {
        public readonly IRepositoryBase<Material> RepMaterial;
        public readonly IRepositoryBase<Units> RepUnits;
        public readonly IRepositoryBase<Products> RepProducts;

        public readonly IMapper mapper;

        public MaterialService(IRepositoryBase<Material> RepMaterial,
            IMapper mapper,
            IRepositoryBase<Products> RepProducts,
            IRepositoryBase<Units> RepUnits) : base(RepMaterial)
        {
            this.RepMaterial = RepMaterial;
            this.mapper = mapper;
            this.RepProducts = RepProducts;
            this.RepUnits = RepUnits;
        }

        /// <summary>
        /// 添加物料
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddMaterial(MaterialRequestDto entity)
        {
            if (entity.MEncode == "")
            {
                //编号自动生成
                entity.MEncode = "IF" + DateTime.Now.ToString("yyyyMMddHHmmss");
            }
            var list = mapper.Map<Material>(entity);
            await RepMaterial.Add(list);
            return new ResultResponseDto<int>();
        }
        /// <summary>
        /// 删除物料
        /// </summary>
        /// <param name="MId"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> DeleleMaterial(int MId)
        {
            await RepMaterial.Delete(MId);
            return new ResultResponseDto<int>();
        }

        /// <summary>
        /// 显示物料列表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<PageResponseDto<List<MaterialResponseDto>>>> GetMaterial(int pageindex,int pagesize,string? mEncode, string? mName, string? pName)
        {
            var list = (await RepMaterial.GetAll()).ToList();
            var listUnit = (await RepUnits.GetAll()).ToList();
            var listProducts = (await RepProducts.GetAll()).ToList();
            var lists = (from a in list 
                         join b in listUnit on a.MUnit equals b.Id
                         join c in listProducts on a.Mproducts equals c.PId
                         select new MaterialResponseDto
                                                                                                 {
                             MId = a.MId,
                             MEncode = a.MEncode,
                             MName = a.MName,
                             MUnit = b.Name,
                             Mproducts = c.PYesNoEnable==false? "物料":"产品",
                             MRemark = a.MRemark,
                             MModelType = a.MModelType,
                             MproductShuosu = c.PName,
                             MSafetyStock = a.MSafetyStock,
                             MYesNoEnable = a.MYesNoEnable,
                             CreateDate = a.CreateDate
                         }).ToList();
            if (!string.IsNullOrEmpty(mEncode))
            {
                lists = lists.Where(o => o.MEncode.Contains(mEncode)).ToList();
            }
            if (!string.IsNullOrEmpty(mName))
            {
                lists = lists.Where(o => o.MName.Contains(mName)).ToList();
            }
            if (!string.IsNullOrEmpty(pName))
            {
                lists = lists.Where(o => o.MproductShuosu.Contains(pName)).ToList();
            }
            var listDto = mapper.Map<List<MaterialResponseDto>>(lists);
            return new ResultResponseDto<PageResponseDto<List<MaterialResponseDto>>> ()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "物料列表",
                Data = new PageResponseDto<List<MaterialResponseDto>>()
                {
                    TotalCount = listDto.Count,
                    PageCount = (int)Math.Ceiling(listDto.Count*1.0 /pagesize),
                    Data = listDto.Skip((pageindex - 1) * pagesize).Take(pagesize).ToList()
                }
            };
        }
        /// <summary>
        /// 反填物料
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<MaterialResponseDto>> GetMaterialById(int Id)
        {
            var list = await RepMaterial.Get(Id);
            var listDto = mapper.Map<MaterialResponseDto>(list);
            return new ResultResponseDto<MaterialResponseDto>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "物料列表",
                Data = listDto
            };
        }
        /// <summary>
        /// 修改物料
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdateMaterial(MaterialRequestDto entity)
        {
            var list = mapper.Map<Material>(entity);
            await RepMaterial.Update(list);
            return new ResultResponseDto<int>();
        }
    }
}
